package com.xc.utils.ip;

import javax.servlet.http.HttpServletRequest;

/**
 * @author xiongcan
 * @version 1.0
 * @date 2018/9/24 下午7:42
 */
public class IpUtils {


    /**
     *  得到ip
     *  使用nginx配置了反向代理，后需要在ngix中增加
     *
     *  proxy_set_header X-real-ip  $remote_addr;
     */
    public static String getIp(HttpServletRequest request){
        if(null == request){
            return "";
        }
        //先从nginx自定义配置获取
        String ip = request.getHeader("X-real-ip");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("x-forwarded-for");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }


}
